package com.heba.greenmulu.diary;

import android.content.res.Resources;
import android.os.Bundle;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.DisplayMetrics;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.heba.adapter.FmPagerAdapter;
import com.heba.fragment.search.SearchAccountFragment;
import com.heba.fragment.search.SearchHashTagFragment;
import com.heba.greenmulu.BaseActivity;
import com.heba.greenmulu.R;
import com.heba.utils.IndicatorLineUtil;
import com.heba.view.ClearEditTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 账户和标签搜索
 * Created by jason on 2020/1/8.
 */
public class ItemDiarySearchNewActivity extends BaseActivity {

    private ClearEditTextView clearEditTextView;
    private TextView tv_cancel;
    private ConstraintLayout bottomLine1;
    private int selectIndex = 0;
    /**
     * tablayout
     */
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private List<String> tabTitles;
    private List<Fragment> fragments = new ArrayList<>();

    private SearchAccountFragment searchAccountFragment;
    private SearchHashTagFragment searchHashTagFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_search);
        findViewById();
        initView();
        initFragment();
        setupClearEditTextView();
        setCustomTabView();
    }

    @Override
    protected void findViewById() {
        clearEditTextView = findViewById(R.id.clearEditText);
        tv_cancel = findViewById(R.id.txt_right);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
        bottomLine1 = findViewById(R.id.bottom_line_1);
    }

    @Override
    protected void initView() {
        tabTitles = new ArrayList<>();
    }

    /**
     * initFragment
     */
    private void initFragment() {
        searchAccountFragment = SearchAccountFragment.newInstance(clearEditTextView.getText());
        searchHashTagFragment = SearchHashTagFragment.newInstance(clearEditTextView.getText());

        fragments.add(searchAccountFragment);
        fragments.add(searchHashTagFragment);
    }

    /**
     * 添加tab标题
     */
    private void addTabTitles() {
        tabTitles.clear();
        tabTitles.add(getResources().getString(R.string.diary_search_account));
        tabTitles.add(getResources().getString(R.string.diary_search_hashtag));
    }

    /**
     * 设置自定义tab
     */
    private void setCustomTabView() {

        /**
         * 添加title文字
         */
        addTabTitles();

        /**
         * 添加tab
         */
        for (int i = 0; i < tabTitles.size(); i++) {
            tabLayout.addTab(tabLayout.newTab());
        }

        /**
         * 关联viewpager,设置adapter
         */
        tabLayout.setupWithViewPager(viewPager, false);
        pagerAdapter = new FmPagerAdapter(fragments, getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);

        /**
         * 设置tab文字,必须在关联viewpager之后设置，否则文字不显示
         */
        for (int i = 0; i < tabTitles.size(); i++) {
            tabLayout.getTabAt(i).setText(tabTitles.get(i));
        }

        /**
         * 设置指示线长度间隔(单位dp)
         */
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                IndicatorLineUtil.setIndicator(tabLayout, 0, 0);
            }
        });
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tabLayout.getSelectedTabPosition();
                Resources resources = getResources();
                DisplayMetrics dm = resources.getDisplayMetrics();
                int width3 = dm.widthPixels;
                TranslateAnimation translateAnimation1 = new TranslateAnimation((selectIndex)*(width3/2),(position)*(width3/2),0,0);
                translateAnimation1.setDuration(300);
                translateAnimation1.setFillEnabled(true);//使其可以填充效果从而不回到原地
                translateAnimation1.setFillAfter(true);//不回到起始位置
                bottomLine1.startAnimation(translateAnimation1);


                translateAnimation1.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        selectIndex = tabLayout.getSelectedTabPosition();

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 设置edittext
     */
    private void setupClearEditTextView() {
        clearEditTextView.setTextHint(this.getResources().getString(R.string.scan_search));

        /**
         * 搜索關鍵詞
         */
        clearEditTextView.setOnEditTextListener(new ClearEditTextView.OnEditTextListener() {
            @Override
            public void onDeleteClick() {
                refreshSearch("");
            }

            @Override
            public void afterTextChanged(String text) {
            }

            @Override
            public void onSearchClick(String text) {
                if (text.trim().isEmpty())
                    return;
                refreshSearch(text);
            }

            @Override
            public void onIntent() {

            }
        });

        tv_cancel.setOnClickListener(v -> finishActivity());
    }

    /**
     * refreshSearch
     *
     * @param searchContent
     */
    private void refreshSearch(String searchContent) {
        if (searchAccountFragment.isVisible()) {
            searchAccountFragment.refreshData(searchContent);
        }
        if (searchHashTagFragment.isVisible()) {
            searchHashTagFragment.refreshData(searchContent);
        }
    }

}
